"""
    Controller for teacher tariff table.
"""

from turbogears import widgets, expose, redirect, validate, identity
from mnemoply import model
from mnemoply.widgets import DictionaryField
from mnemoply.controllers.lookup import LookupController, TableCol, SQLObjectLookupDataSource
from mnemoply.controllers.crud import CRUDController, ItemTab

class AdditionsTab(ItemTab):
    title = _("Quantity additions")
    name = "addition_tab"
    template = "../quantity_addition_teacher_tariff"

    add_form = widgets.TableForm(fields=[
            DictionaryField(
                name="addition_id",
                model_object_name='QuantityAddition',
                not_empty=True,
                label="Quantity addition",
            )
        ],
        submit_text=_("Add"),
        action='add_addition',
    )

    @identity.require(identity.has_any_permission('dba', 'TeacherTariff/edit'))
    @expose()
    def delete_addition(self, obj, qt_id):
        model.QuantityAdditionTeacherTariff.get(int(qt_id)).destroySelf()
        raise redirect("./")

    @identity.require(identity.has_any_permission('dba', 'TeacherTariff/edit'))
    @validate(form=add_form)
    @expose()
    def add_addition(self, obj, addition_id):
        addition = model.QuantityAddition.get(int(addition_id))
        model.QuantityAdditionTeacherTariff(tariff=obj, addition=addition)

        raise redirect("./")

class TeacherTariffController(LookupController, CRUDController):
    model_object = model.TeacherTariff
    lookup_columns = [
        TableCol(name='name', caption=_('Name')),
        TableCol(name='complexityAddition', caption=_('Complexity addition')),
    ]
    view_fields = ['name', 'complexityAddition', 'lessonAssistantTypeID']
    item_tabs = [AdditionsTab()]

    def __init__(self):
        LookupController.__init__(self,
            data_source=SQLObjectLookupDataSource(self.model_object),
            columns=self.lookup_columns,
            title=self.model_object.model_title,
        )
        CRUDController.__init__(self, model_object=self.model_object)

exposed = TeacherTariffController()
